import menuTpl from '../views/menu.html'
import "../controller/userController"
import "../router" //等同于 ../router/index
import router from '../router';

class Index {
  constructor() {
    this._initEvent()
    this.render();
    this.startSocket();
  }

  render() {
    $("#menu-container").html(menuTpl)
  }

  _initEvent() {
    $.ajaxSetup({
      beforeSend(xhr, setting) {
        let token = localStorage.getItem('token');
        xhr.setRequestHeader('x-access-token', token);
      },
      complete(xhr, setting) {
        if (xhr.responseJSON.code === 401) {
          alert(xhr.responseJSON.message)
          router.go('/index')
        }
      }
    })
  }

  startSocket() {
    var socket = io("http://localhost:3000");
    socket.on('connect', function () {
      console.log('connect ...')
    })

    socket.on('message', function (msg) {
      console.log(msg)
    })
  }

}

new Index();